package com.example.monitorsms;

import android.app.Dialog;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import com.example.monitorsms.http.CommonHttpConsts;
import com.example.monitorsms.http.HttpCallbackData;
import com.example.monitorsms.http.HttpClient;
import com.example.monitorsms.utils.DialogUitl;
import com.example.monitorsms.utils.SPUtils;
import com.example.monitorsms.utils.StringUtils;

public class RegisterActivity extends BaseActivity {
    private EditText et_account, et_password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        super.main(savedInstanceState);
        et_account = findViewById(R.id.et_account);
        et_password = findViewById(R.id.et_account);

        findViewById(R.id.imgBack).setOnClickListener(v -> finish());
        findViewById(R.id.btnRegister).setOnClickListener(v -> {
            String account = et_account.getText().toString().trim();
            String password = et_password.getText().toString().trim();
            if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password)) {
                Toast.makeText(mContext, "账号、密码不能为空！", Toast.LENGTH_SHORT).show();
            }
            if (!StringUtils.isMobileNO(account)) {
                Toast.makeText(mContext, "手机号输入格式不正确！", Toast.LENGTH_SHORT).show();
                return;
            }
            register(account, password);
        });
    }

    private void register(String account, String password) {
        HttpClient.getInstance().postData(CommonHttpConsts.register, "register")
                .params("mobile", account)
                .params("password", password)
                .execute(new HttpCallbackData() {
                    @Override
                    public void onSuccess(int code, String msg, String info) {
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                        if (code == 1) {
                            SPUtils.put(mContext, "userType", true);
                            RegisterActivity.this.finish();
                        }
                    }

                    @Override
                    public boolean showLoadingDialog() {
                        return true;
                    }

                    @Override
                    public Dialog createLoadingDialog() {
                        return DialogUitl.loginAuthDialog(mContext);
                    }
                });
    }
}
